/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 2013-2014 sagyf Yang. The Four Group.
 */

package app.models.sys;

import app.models.DaoKit;
import com.github.sog.annotation.TableBind;
import com.github.sog.config.StringPool;
import com.github.sog.controller.datatables.core.DatatablesCriterias;
import com.github.sog.kit.date.DateProvider;
import com.github.sog.plugin.sqlinxml.SqlKit;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import java.util.Date;

/**
 * <p>
 * .
 * </p>
 *
 * @author sagyf yang
 * @version 1.0 2014-05-20 22:03
 * @since JDK 1.6
 */
@TableBind(tableName = "ol_sms_record")
public class SmsRecord extends Model<SmsRecord> {
    /**
     * The public dao.
     */
    public static final SmsRecord dao = new SmsRecord();


    private static final long serialVersionUID = 5851252301512527267L;

    public void recore(String phone, String catcha, int time, String message) {
        SmsRecord record = new SmsRecord();
        record.set("send_phone", phone);
        final Date currentDate = DateProvider.DEFAULT.getCurrentDate();
        record.set("send_time", currentDate);
        record.set("sms_url", "SendMessage");
        record.set("sms_param", "{\"catcha\": \"" + catcha + "\", \"time\": \"" + time + "\"}");
        record.set("last_send_time", currentDate);
        record.set("create_time", currentDate);
        record.set("exception", message);
        record.save();

    }

    public void recordAndResult(String phone, String conent, String result, String param){
        recordAndResultWithException(phone, conent, result, StringPool.EMPTY, param);
    }

    public void recordAndResultWithException(String phone, String content, String result, String exception, String param){
        SmsRecord record = new SmsRecord();
        record.set("send_phone", phone);
        final Date currentDate = DateProvider.DEFAULT.getCurrentDate();
        record.set("send_time", currentDate);
        record.set("send_content", content);
        record.set("return_uuid", result);
        record.set("return_status", result);
        record.set("sms_url", "SendMessage");
        record.set("sms_param", param);
        record.set("last_send_time", currentDate);
        record.set("create_time", currentDate);
        record.set("exception", exception);
        record.save();
    }

    public Page<Record> datatable(DatatablesCriterias criterias) {
        return DaoKit.paginate(SqlKit.sql("smsrecord.where"),
                SqlKit.sql("smsrecord.columns"), criterias,
                SqlKit.sql("smsrecord.orders"), null);
    }
}